package com.android.lovingpet.common.postlist;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.common.UserModel;
import com.android.lovingpet.common.PostModel;

import java.util.ArrayList;
import java.util.List;

public class PostListPresenter extends BasePresenter<PostListContract.View> implements PostListContract.Presenter {

    private int mode;

    /**
     * @param mode 0：根据type获取分类帖子，1根据用户id获取帖子，2获取关注的人动态，-1为你推荐
     */
    public PostListPresenter(int mode) {
        this.mode = mode;
        if (mode != -1 && mode != 0 && mode != 1 && mode != 2) {
            throw new IllegalArgumentException("非法启动模式");
        }
    }

    @Override
    public void getPosts(int typeOrUserId, int offset) {
        if (offset == 0) {
            getView().clearPosts();
        }
        if (mode == 0) {
            getTypePosts(typeOrUserId, 0, offset, null);
        } else if (mode == 1) {
            getUserPosts(typeOrUserId, offset);
        } else if (mode == 2) {
            getTypePosts(5, 3, offset, 1);
        } else if (mode == -1) {
            for (int i = 1; i <= 4; i++) {
                PostModel.getInstance().getIdsByType(i, 0, 0, 10, null, getBack());
            }
        }
    }

    private void getUserPosts(int userId, int offset) {
        PostModel.getInstance().getIdsByUser(userId, 3, offset, 10, getBack());
    }

    private void getTypePosts(int type, int postType, int offset, Integer chooseType) {
        PostModel.getInstance().getIdsByType(type, postType, offset, 10, chooseType, getBack());
    }

    private void getPost(List<Integer> ids) {
        List<Post> posts = new ArrayList<>();
        for (int i : ids) {
            PostModel.getInstance().getPost(i, 0, 1, new Back() {
                @Override
                public void onFailure(String msg) {
                    if (getView() == null)
                        return;
                    getView().complete();
                    getView().showToast(msg);
                }

                @Override
                public void onSuccess(@Nullable Bundle bundle) {
                    if (getView() == null)
                        return;
                    Post post = bundle.getParcelable("post");

                    UserModel.getInstance().getType(post.getUserId(), new Back() {
                        @Override
                        public void onFailure(String msg) {
                            getView().showToast(msg);
                        }

                        @Override
                        public void onSuccess(@Nullable Bundle bundle) {
                            int type = bundle.getInt("type");
                            post.setUserType(type);
                            if (type == 0)
                                getTeam(post.getUserId());
                            else
                                getUser(post.getUserId());
                            posts.add(post);
                            if (posts.size() == ids.size()) {
                                getView().addPosts(posts);
                                getView().complete();
                            }
                        }
                    });

                }
            });
        }
    }

    private void getTeam(int teamId) {
        UserModel.getInstance().getTeamInfo(teamId, new FileModel.FileBack() {
            private Team team;

            @Override
            public void loadSuccess(String url) {
                team.setImage(url);
                team.saveOrUpdate();
                getView().setUser();
            }

            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                team = bundle.getParcelable("team");
                team.saveOrUpdate();
                getView().setUser();
            }
        });
    }

    private void getUser(int userId) {
        UserModel.getInstance().getInfo(userId, new FileModel.FileBack() {
            private Account account;

            @Override
            public void loadSuccess(String url) {
                if (getView() == null)
                    return;
                account.setHead(url);
                account.saveOrUpdate();
                getView().setUser();
            }

            @Override
            public void onFailure(String msg) {
                if (getView() == null)
                    return;
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (getView() == null)
                    return;
                account = bundle.getParcelable("account");
                account.saveOrUpdate();
                getView().setUser();
            }
        });
    }

    private Back getBack() {
        return new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView() == null)
                    return;
                getView().complete();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                List<Integer> ids = bundle.getIntegerArrayList("ids");
                if (ids == null || ids.isEmpty()) {
                    if (getView() == null)
                        return;
                    getView().complete();
                    return;
                }
                getPost(ids);
            }
        };
    }
}
